package com.example.knowledgeget1.KnowtCommon.Service;

import com.example.knowledgeget1.KnowtCommon.Entity.Teach;
import com.example.knowledgeget1.KnowtCommon.Entity.Teacher;
import com.example.knowledgeget1.KnowtCommon.Repository.TeachRepository;
import com.example.knowledgeget1.KnowtCommon.Repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class TeachService {
    @Autowired
    private TeachRepository teachRepository;
    @Autowired
    private TeacherRepository teacherRepository;

    // 根据教师ID获取教师姓名
    public Teacher getTeacherNameById(Long teaId) {
        return teacherRepository.findByTeaId(teaId);
    }

    // 创建新教学记录
    public Teach createTeach(Teach teach) {
        return teachRepository.save(teach);
    }

    // 获取所有教学记录
    public List<Teach> getAllTeaches() {
        return teachRepository.findAll();
    }

    // 通过ID获取单个教学记录
    public Teach getTeachById(Long id) {
        Optional<Teach> teach = teachRepository.findById(id);
        return teach.orElse(null);
    }

    // 更新教学记录信息
    public Teach updateTeach(Long id, Teach teachDetails) {
        Teach teach = getTeachById(id);
        if (teach != null) {
            teach.setTeaId(teachDetails.getTeaId());
            teach.setCourseId(teachDetails.getCourseId());
            // 更新其他字段...
            teachRepository.save(teach);
        }
        return teach;
    }

    // 删除教学记录
    public void deleteTeach(Long id) {
        teachRepository.deleteById(id);
    }
}